package com.garen.sys.biz.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.garen.common.MsgBean;
import com.garen.common.PageBean;
import com.garen.sys.biz.IFireUserBiz;
import com.garen.sys.dao.ICommonDao;
import com.garen.sys.dao.IFireOrgDao;
import com.garen.sys.dao.IFireUserDao;
import com.garen.sys.entity.FireOrg;
import com.garen.sys.entity.FireUser;
import com.garen.utils.CommonUtils;


@Service
public class FireUserBizImpl implements IFireUserBiz {

	protected static Log log = LogFactory.getLog(FireUserBizImpl.class);

	
	@Autowired
	private IFireUserDao fireUserDao;

	@Autowired
	private IFireOrgDao fireOrgDao;

	

	@Autowired
	private ICommonDao commonDao;

	private FireOrg saveOrg(Map<String,Object>map,FireOrg org){
		org.setOrgAllName(map.get("name") + "");
		org.setOrgShortName(map.get("shortname") + "");
		org.setOrgCode(map.get("flag") + "");
		Object lat = map.get("org_lat");
		Object lng = map.get("org_lng");
		if(lat != null && lng != null){
			org.setOrgLat(lat.toString());
			org.setOrgLng(lng.toString());
		}
		return org;
	}
	
	public void test(){
		List<Map<String,Object>> list1 = commonDao.allTable();
		for(Map<String,Object>map1 : list1){
			log.debug(map1.get("table_name"));
		}
	}

	@Override
	public FireUser login(MsgBean mbean, String loginName,String password) {
		FireUser user = new FireUser();
		//user.setLoginName(loginName);
		user.setUserLoginName(loginName);
		//密码加密
		user.setUserPassword(CommonUtils.encryptSHA(password));
		user = fireUserDao.get(user);
		if(user != null){
			if("0".equals(user.getUserStatus())){
				mbean.setRetInfo(-1, "账号已经停用,请与管理员联系!");
				return null;
			}
			FireUser u = new FireUser();
			u.setUserId(user.getUserId());
			u.setLastLoginTime(new Date());
			u.setLastLoginTime(new Date());//更新登录时间
			fireUserDao.update(u);
			mbean.setRetInfo(0, "登录成功");
		}else{
			mbean.setRetInfo(-30, "帐号或密码错误");
		}
		return user;
	}

	
	/*
	*分页查询
	*/
	@Override
	public List<FireUser> getFireUserList(PageBean<FireUser> pb,FireUser fireUser){
		return fireUserDao.getPageList(pb,fireUser);
	}

	/*
	*分页查询
	*/
	@Override
	public List<FireUser> getFireUserList(FireUser fireUser){
		return fireUserDao.getList(fireUser);
	}

	/*
	*查询个体
	*/
	@Override
	public FireUser getFireUser(FireUser fireUser){
		return fireUserDao.get(fireUser);
	}

	/*
	*保存
	*/
	@Override
	public int saveFireUser(MsgBean mbean,FireUser fireUser){
		int ret = -1;
		if(fireUserDao.checkLoginName(fireUser.getUserId(),fireUser.getUserLoginName()) > 0){
			mbean.setRetInfo(-2, "登录帐号已经使用 !");
			return -1;
		}
		if(fireUser.getUserId() == null){
			fireUser.setUserPassword(FireUser.DEFAULT_MM);
			fireUser.setCreateDate(new Date());
			ret = fireUserDao.add(fireUser);
		}else{
			ret = fireUserDao.update(fireUser);
		}
		return ret;
	}

	/*
	*删除
	*/
	@Override
	public int delFireUser(MsgBean mbean,FireUser fireUser){
		return fireUserDao.del(fireUser);
	}

	@Override
	public List<Map<String, Object>> getcountandduty(String org_id) {
		return fireUserDao.getcountandduty(org_id);
	}

}
